home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / command / install_lib.py < prev    next >
Encoding:
Python Source  |  2000-08-05  |  5.4 KB  |  153 lines

  1. # created 1999/03/13, Greg Ward
  2.  
  3. __revision__ = "$Id: install_lib.py,v 1.27 2000/08/05 01:31:54 gward Exp $"
  4.  
  5. import sys, os, string
  6. from distutils.core import Command
  7. from distutils.dir_util import copy_tree
  8.  
  9. class install_lib (Command):
  10.  
  11.     description = "install all Python modules (extensions and pure Python)"
  12.  
  13.     user_options = [
  14.         ('install-dir=', 'd', "directory to install to"),
  15.         ('build-dir=','b', "build directory (where to install from)"),
  16.         ('compile', 'c', "compile .py to .pyc"),
  17.         ('optimize', 'o', "compile .py to .pyo (optimized)"),
  18.         ('skip-build', None, "skip the build steps"),
  19.         ]
  20.                
  21.  
  22.     def initialize_options (self):
  23.         # let the 'install' command dictate our installation directory
  24.         self.install_dir = None
  25.         self.build_dir = None
  26.         self.compile = 1
  27.         self.optimize = 1
  28.         self.skip_build = None
  29.  
  30.     def finalize_options (self):
  31.  
  32.         # Get all the information we need to install pure Python modules
  33.         # from the umbrella 'install' command -- build (source) directory,
  34.         # install (target) directory, and whether to compile .py files.
  35.         self.set_undefined_options ('install',
  36.                                     ('build_lib', 'build_dir'),
  37.                                     ('install_lib', 'install_dir'),
  38.                                     ('compile_py', 'compile'),
  39.                                     ('optimize_py', 'optimize'),
  40.                                     ('skip_build', 'skip_build'),
  41.                                    )
  42.  
  43.  
  44.     def run (self):
  45.  
  46.         # Make sure we have built everything we need first
  47.         if not self.skip_build:
  48.             if self.distribution.has_pure_modules():
  49.                 self.run_command ('build_py')
  50.             if self.distribution.has_ext_modules():
  51.                 self.run_command ('build_ext')
  52.  
  53.         # Install everything: simply dump the entire contents of the build
  54.         # directory to the installation directory (that's the beauty of
  55.         # having a build directory!)
  56.         if os.path.isdir(self.build_dir):
  57.             outfiles = self.copy_tree (self.build_dir, self.install_dir)
  58.         else:
  59.             self.warn("'%s' does not exist -- no Python modules to install" %
  60.                       self.build_dir)
  61.             return
  62.  
  63.         # (Optionally) compile .py to .pyc
  64.         # XXX hey! we can't control whether we optimize or not; that's up
  65.         # to the invocation of the current Python interpreter (at least
  66.         # according to the py_compile docs).  That sucks.
  67.  
  68.         if self.compile:
  69.             from py_compile import compile
  70.  
  71.             for f in outfiles:
  72.                 # only compile the file if it is actually a .py file
  73.                 if f[-3:] == '.py':
  74.                     out_fn = f + (__debug__ and "c" or "o")
  75.                     compile_msg = "byte-compiling %s to %s" % \
  76.                                   (f, os.path.basename (out_fn))
  77.                     skip_msg = "skipping byte-compilation of %s" % f
  78.                     self.make_file (f, out_fn, compile, (f,),
  79.                                     compile_msg, skip_msg)
  80.     # run ()
  81.  
  82.  
  83.     def _mutate_outputs (self, has_any, build_cmd, cmd_option, output_dir):
  84.  
  85.         if not has_any:
  86.             return []
  87.  
  88.         build_cmd = self.get_finalized_command (build_cmd)
  89.         build_files = build_cmd.get_outputs()
  90.         build_dir = getattr (build_cmd, cmd_option)
  91.  
  92.         prefix_len = len (build_dir) + len (os.sep)
  93.         outputs = []
  94.         for file in build_files:
  95.             outputs.append (os.path.join (output_dir, file[prefix_len:]))
  96.  
  97.         return outputs
  98.  
  99.     # _mutate_outputs ()
  100.  
  101.     def _bytecode_filenames (self, py_filenames):
  102.         bytecode_files = []
  103.         for py_file in py_filenames:
  104.             bytecode = py_file + (__debug__ and "c" or "o")
  105.             bytecode_files.append(bytecode)
  106.  
  107.         return bytecode_files
  108.         
  109.     def get_outputs (self):
  110.         """Return the list of files that would be installed if this command
  111.         were actually run.  Not affected by the "dry-run" flag or whether
  112.         modules have actually been built yet."""
  113.  
  114.         pure_outputs = \
  115.             self._mutate_outputs (self.distribution.has_pure_modules(),
  116.                                   'build_py', 'build_lib',
  117.                                   self.install_dir)
  118.         if self.compile:
  119.             bytecode_outputs = self._bytecode_filenames(pure_outputs)
  120.         else:
  121.             bytecode_outputs = []
  122.  
  123.         ext_outputs = \
  124.             self._mutate_outputs (self.distribution.has_ext_modules(),
  125.                                   'build_ext', 'build_lib',
  126.                                   self.install_dir)
  127.  
  128.         return pure_outputs + bytecode_outputs + ext_outputs
  129.  
  130.     # get_outputs ()
  131.  
  132.     def get_inputs (self):
  133.         """Get the list of files that are input to this command, ie. the
  134.         files that get installed as they are named in the build tree.
  135.         The files in this list correspond one-to-one to the output
  136.         filenames returned by 'get_outputs()'."""
  137.  
  138.         inputs = []
  139.         
  140.         if self.distribution.has_pure_modules():
  141.             build_py = self.get_finalized_command ('build_py')
  142.             inputs.extend (build_py.get_outputs())
  143.  
  144.         if self.distribution.has_ext_modules():
  145.             build_ext = self.get_finalized_command ('build_ext')
  146.             inputs.extend (build_ext.get_outputs())
  147.  
  148.         return inputs
  149.             
  150.         
  151.  
  152. # class install_lib
  153.